package aop05;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

public class LogAdvice {
    public void before(JoinPoint jp){
        Object target = jp.getTarget();
        Signature signature = jp.getSignature();
        Object[] args = jp.getArgs();
        System.out.println("在"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"执行了"+signature.getName()+"方法，参数为"+ Arrays.toString(args));
    }

    public void afterReturn(JoinPoint jp,Object resultValue){
        Object target = jp.getThis();
        Signature signature = jp.getSignature();
        Object[] args = jp.getArgs();
        System.out.println("在"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"执行了"+signature.getName()+"方法，方法返回值为"+ resultValue);
    }
    public void afterThrowing(JoinPoint jp,Exception e){
        Object target = jp.getThis();
        Signature signature = jp.getSignature();
        Object[] args = jp.getArgs();
        System.out.println("在"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"执行了"+signature.getName()+"方法，出现异常为"+ e.getMessage());

    }
    public void after(JoinPoint jp){
        Object target = jp.getThis();
        Signature signature = jp.getSignature();
        Object[] args = jp.getArgs();
        System.out.println("在"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"执行了"+signature.getName()+"方法，参数为"+ Arrays.toString(args));

    }

}
